<?php
/**
 * 《产品生产》实现类
 *
 * @author 孙晓晔
 * @version $Id: Produce.class.php 1037 2010-03-26 11:47:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/MoldUtil.class.php');

class Produce extends MoldUtil {
	var $info_dir = 'produce';

	public $file = '3';

	public $tbl = Constant::tbl_mold_produce;

	public $quality = array(
		'-' => '请选择',
		'1' => '需要',
		'2' => '不需要',
	);

	public $status = array(
		'-' => '请选择',
		'1' => '等待质检',
		'2' => '正在质检',
		'3' => '重新质检',		

		'4' => '暂停质检',
		'5' => '取消质检',	

		'8' => '质检异常',
		'9' => '质检合格',

		'99' => '不需质检',
	);

	public $groupby = array(
		'-' => '请选择',
		'mold' => '产品',
		'emp' => '负责人',
		'status' => '状态',
		'1' => '日期',
		'2' => '月份',
	);

	public $orderby = array(
		'-' => '请选择',
		'mold' => '产品',
		'technics' => '工艺',
		'emp' => '负责人',
		't2' => '完成日期',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('quality_option', $this->quality);
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('-2 month'));

		$controller->display($request, 'mold/produce/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		if ($px == 't2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['t2'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$plan_s = $request['plan_s'];

		$piece_s = $request['piece_s'];
		$technics_s = $request['technics_s'];

		$work_s = $request['work_s'];

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " where " . $my;
		}

		if (!empty($plan_s)) {
			$where .= " AND plan = '" . $plan_s . "'";
		}

		if (!empty($piece_s) && $piece_s <> '-') {
			$where .= " AND piece = '" . $piece_s . "'";
		}

		if (!empty($technics_s) && $technics_s <> '-') {
			$where .= " AND technics = '" . $technics_s . "'";
		}

		if (!empty($work_s) && $work_s <> '-') {
			$where .= " AND work = '" . $work_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];

				$row['mold_t'] = Name::mold($row['mold']);

				$mold = $this->id(Constant::tbl_mold, $row['mold']);
				$row['engineer_t'] = Name::emp($mold['engineer']);

				$row['piece'] = $this->piece[$row['piece']];
				$row['technics'] = $this->technics[$row['technics']];
				$row['work'] = $this->work[$row['work']];

				$row['file'] = $this->_file_exist($row['id']);

				$row['emp_t'] = Name::emp($row['emp']);
				$row['quality_t'] = Name::emp($row['quality']);

				$time = $row['tt'];

				$row['hour'] = floor($time / 60);
				$row['minute'] = $time % 60;

				$row['status_t'] = $this->status[$row['status']];

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'mold/produce/list');
		return true;
	}

	/**
	 * 添加
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		require_once 'Plan.class.php';
		$plan = new Plan();
		$data = $plan->_view($id);
		$controller->assign_by_ref('plan', $data);

		$this->time($controller);		

		$controller->assign('t1', strtotime($data['modifytime']));

		$controller->display($request, 'mold/produce/add');
		return true;
	}

	/**
	 * 添加
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$mold = $request['mold'];
			
		$piece = $request['piece'];
		$technics = $request['technics'];
		$work = $request['work'];

		$content = addslashes($request['content']);

		$quality = $request['quality'];
		if ($quality == '1') {
			$status = '1';
		} else {
			$status = '99';
		}

		$t1 = $request['t1'];
		$t2 = $request['t2'];

		$hour = $request['hour'];
		$minute = $request['minute'];
		$tt = $hour * 60 + $minute;

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(plan, mold, piece, technics, work, content, t1, t2, tt, emp, status, createtime) ";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now())";
		$sql = sprintf($fmt, $this->tbl, $id, $mold, $piece, $technics, $work, $content, $t1, $t2, $tt, $_SESSION['id'], $status);
		// debug($sql);
		$db->query($sql);

		$ret = array('id'=> $db->insert_id());

		$this->plan($id);

		echo json_encode($ret);
		return true;
	}

	function plan($id) {
		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET status = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, Constant::tbl_mold_plan, '9', $id);
		// debug($sql);
		$db->query($sql);

		clean(Constant::tbl_mold_plan . "_id_" . $id);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$row['piece'] = $this->piece[$row['piece']];
		$row['technics'] = $this->technics[$row['technics']];

		$time = $row['tt'];

		$row['hour'] = floor($time / 60);
		$row['minute'] = $time % 60;

		$row['status_t'] = $this->status[$row['status']];

		$row['file'] = Download::url($this->info_dir, $row['createtime'], $row['plan'], $row['filename']);
		$row['emp_t'] = Name::emp($row['emp']);

		$controller->assign_by_ref('result', $row);

		$this->time($controller);

		require_once 'Mold.class.php';
		$mold = new Mold();
		$controller->assign_by_ref('mold', $mold->_view($row['mold']));

		$plan = $this->id(Constant::tbl_mold_plan, $row['plan']);
		$plan['emp_t'] = Name::emp($plan['emp']);
		$controller->assign_by_ref('plan', $plan);

		$controller->display($request, 'mold/produce/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$content = addslashes($request['content']);
		$t1 = $request['t1'];
		$t2 = $request['t2'];

		$hour = $request['hour'];
		$minute = $request['minute'];
		$tt = $hour * 60 + $minute;

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET content = '%s', t1 = '%s', t2 = '%s', tt = '%s'";
		$fmt .= ", modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $content, $t1, $t2, $tt, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$row['piece'] = $this->piece[$row['piece']];
		$row['technics'] = $this->technics[$row['technics']];

		$time = $row['tt'];

		$row['hour'] = floor($time / 60);
		$row['minute'] = $time % 60;

		$row['emp_t'] = Name::emp($row['emp']);

		$row['status_t'] = $this->status[$row['status']];

		$controller->assign_by_ref('result', $row);

		$this->time($controller);

		require_once 'Mold.class.php';
		$mold = new Mold();
		$controller->assign_by_ref('mold', $mold->_view($row['mold']));

		require_once 'Plan.class.php';
		$plan = new Plan();
		$controller->assign_by_ref('plan', $plan->_view($row['plan']));

		$controller->assign_by_ref('file', $this->_file($row));

		$controller->display($request, 'mold/produce/view');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function _view($id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s where plan = '%s'";
		$sql = sprintf($fmt, Constant::tbl_mold_produce, $id);
		// debug($sql);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);

		$this->piece = $this->code_option("2");
		$this->technics = $this->code_option("3");

		$row['piece'] = $this->piece[$row['piece']];
		$row['technics'] = $this->technics[$row['technics']];

		$time = $row['tt'];

		$row['hour'] = floor($time / 60);
		$row['minute'] = $time % 60;

		$row['file'] = Download::url($this->info_dir, $row['createtime'], $row['plan'], $row['filename']);
		$row['emp_t'] = Name::emp($row['emp']);

		$row['quality_t'] = Name::emp($row['quality']);

		$row = $this->_view_t($row);

		return $row;
	}

	/**
	 * 更新状态
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function status($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$emp = $request['emp'];
		if (empty($emp)) {
			$emp = $_SESSION['id'];
		}

		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET quality = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $emp, $status, $id);
		// debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}

			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
			if ($row['status'] > 1) {
				continue;
			}

			$fmt = "delete from %s WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $id);
			// debug($sql);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);

		$controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'mold/produce/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'mold');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'mold') {
			$row['a'] = sprintf("%s [%s]", Name::mold($row['a']), $row['a']);
		} else {

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}

	function _bar($controller, $request) {
		$this->pretreat($controller);

		$barcode_s = $request['barcode_s'];
		$aa = explode('.', $barcode_s);

		$id = $aa[2];

		$mold_b = $aa[1];
		$emp_b = $aa[3];
		$status_b = $aa[4];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);
		if (!$row) {
			$controller->assign("msg", "条码有误，请联系管理员");
			$controller->display($request, 'info');
			return false;
		}

		if ($row['status'] == '1' && $status_b == '3') {
			$controller->assign("msg", "质检未开工，不能扫描完工条码，请重新操作");
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign("status", $status_b);
		/*
		 if ($row['emp'] <> $emp_b) {
			$controller->assign("msg", "条码有误，请联系管理员");
			$controller->display($request, 'info');
			return false;
			}*/

		if ($row['mold'] <> $mold_b) {
			$controller->assign("msg", "条码有误，请联系管理员");
			$controller->display($request, 'info');
			return false;
		}

		$row['piece'] = $this->piece[$row['piece']];
		$row['technics'] = $this->technics[$row['technics']];

		$row['emp_t'] = Name::emp($row['emp']);
		$row['quality_t'] = Name::emp($row['quality']);

		$time = $row['tt'];

		$row['hour'] = floor($time / 60);
		$row['minute'] = $time % 60;

		$row['status_t'] = $this->status[$row['status']];

		$controller->assign_by_ref('result', $row);

		require_once 'Mold.class.php';
		$mold = new Mold();
		$controller->assign_by_ref('mold', $mold->_view($row['mold']));

		require_once 'Plan.class.php';
		$plan = new Plan();
		$controller->assign_by_ref('plan', $plan->_view($row['plan']));

		$controller->display($request, 'mold/produce/barcode');
		return true;
	}

	function ajax_b($controller, $request) {
		$id = $_COOKIE['id_'];
		$data = array();
		if (!$this->ajax_b_($id)) {
			$data['status'] = 2;
		} else {
			$data['status'] = 1;
		}

		$controller->assign('result', json_encode($data));

		$request['tpl'] = 'json';
		$controller->display($request, 'json');
		return true;
	}

	function ajax_b_($id) {
		if ($_SESSION['dept'] == 'mjqc') {
			return true;
		}

		if ($this->_is_admin()) {
			return true;
		}

		return false;
	}

	function _report_status($id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT status, COUNT(status) c FROM %s WHERE mold = '%s' GROUP BY status";
		$sql = sprintf($fmt, $this->tbl, $id);
		// debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['status_t'] = $this->status[$row['status']];

			$ret[] = $row;
		}

		return $ret;
	}
}
?>